package com.study;

import com.study.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {

    public static void main(String[] args) {
        // 从键盘获取
        Scanner sc = new Scanner(System.in);

        // 用来存储添加的学生信息
        ArrayList<Student> list = new ArrayList<>();

        lo:
        while (true) {
            System.out.println("------------欢迎来到学生管理系统------------");
            System.out.println("1 添加学生");
            System.out.println("2 查看学生");
            System.out.println("3 修改学生");
            System.out.println("4 删除学生");
            System.out.println("5 退出");
            System.out.println("请输入您的选择：");
            int func = sc.nextInt();
            switch (func) {
                case 1:
                    addStudent(list);
                    break;
                case 2:
                    queryStudent(list);
                    break;
                case 3:
                    modifyStudent(list);
                    break;
                case 4:
                    deleteStudent(list);
                    break;
                case 5:
                    System.out.println("谢谢您的使用！");
                    break lo;
            }
        }
    }

    private static void modifyStudent(ArrayList<Student> list) {
        System.out.println("请输入您要修改的学号：");
        Scanner sc = new Scanner(System.in);
        String sid = sc.next();
        int index = getIndex(list, sid);
        if (index == -1) {
            System.out.println("未找到您要修改的学生信息");
            return;
        } else {
            Student st = list.get(index);
            System.out.println("请输入修改后的姓名：");
            String name = sc.next();
            st.setName(name);
            System.out.println("请输入修改后的姓名：");
            int age = sc.nextInt();
            st.setAge(age);
            System.out.println("请输入修改后的姓名：");
            String birthday = sc.next();
            st.setBirthday(birthday);
            System.out.println("修改成功");
        }
    }

    private static void deleteStudent(ArrayList<Student> list) {
        System.out.println("请输入您要删除的学号：");
        Scanner sc = new Scanner(System.in);
        String sid = sc.next();
        int index = getIndex(list, sid);
        if (index == -1) {
            System.out.println("您输入的学号不存在");
        } else {
            list.remove(index);
            System.out.println("删除成功");
        }

    }

    private static void queryStudent(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("无学生信息，请添加后重试！");
            return;
        }
        // 如果有学生信息，先打印表头信息
        System.out.println("学号\t姓名\t年龄\t生日");
        for (int i = 0; i < list.size(); i++) {
            Student st = list.get(i);
            System.out.println(st.getSid() + "\t" + st.getName() + "\t" + st.getAge() + "\t" + st.getBirthday());
        }
    }

    private static void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学号");
        String sid;
        while (true) {
            sid = sc.next();
            if(getIndex(list, sid)==-1) {
                break;
            } else {
                System.out.println("您输入的学号已存在，请重新输入：");
            }
        }

        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        System.out.println("请输入生日");
        String birthday = sc.next();
        Student stu = new Student(sid, name, age, birthday);
        list.add(stu);
        System.out.println("学生添加成功");
    }

    /*
        通过学号获取集合中的位置
     */
    public static int getIndex(ArrayList<Student> list, String sid) {
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            Student st = list.get(i);
            if(st.getSid().equals(sid)) {
                index = i;
            }
        }
        return index;
    }
}
